package com.wjh.kt_wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.wjh.kt_vod.exception.GgkException;
import com.wjh.kt_vod.result.Result;
import com.wjh.kt_vod.vo.wechat.MenuVo;
import com.wjh.kt_wechat.Utils.ConstantPropertiesUtils;
import com.wjh.kt_wechat.Utils.HttpClientUtils;
import com.wjh.kt_wechat.entity.Menu;
import com.wjh.kt_wechat.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 前端控制器
 * </p>
 *
 * @author Wang_Junhong
 * @since 2022-07-17
 */

@Api(tags = "公众号")
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    /**
     * 同步菜单方法
     * @return
     */
    @GetMapping("/syncMenu")
    public Result create(){
        menuService.syncMenu();
        return Result.ok();

    }

    /**
     * 删除菜单
     * @return
     */
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("removeMenu")
    public Result removeMenu() {
        menuService.removeMenu();
        return Result.ok(null);
    }

    /**
     * 获取 token
     * @return
     */
    @GetMapping("/getAccessToken")
    public Result getAccessToken(){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("https://api.weixin.qq.com/cgi-bin/token");
        stringBuffer.append("?grant_type=client_credential");
        stringBuffer.append("&appid=%s");
        stringBuffer.append("&secret=%s");
        String url = String.format(stringBuffer.toString(), ConstantPropertiesUtils.ACCESS_KEY_ID,ConstantPropertiesUtils.ACCESS_KEY_SECRET);
        try {
            String tokenString = HttpClientUtils.get(url);
            JSONObject jsonObject = JSONObject.parseObject(tokenString);
            String access_token = jsonObject.getString("access_token");
            return Result.ok(access_token);
        } catch (Exception e) {
            throw new GgkException(20001,"获取token失败");
        }

    }


    //获取所有菜单，按照一级和二级菜单封装
    @ApiOperation(value = "获取所有菜单，按照一级和二级菜单封装")
    @GetMapping("findMenuInfo")
    public Result findMenuInfo() {
        List<MenuVo> list = menuService.findMenuInfo();
        return Result.ok(list);
    }

    //获取所有一级菜单
    @ApiOperation(value = "获取所有一级菜单")
    @GetMapping("findOneMenuInfo")
    public Result findOneMenuInfo() {
        List<Menu> list = menuService.findMenuOneInfo();
        return Result.ok(list);
    }

    @ApiOperation(value = "获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        return Result.ok(menu);
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.ok(null);
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public Result updateById(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.ok(null);
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        menuService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        menuService.removeByIds(idList);
        return Result.ok(null);
    }
}

